//计划控制器
define('salesIndexController', ['angular','jquery','datetimepicker','moment'], function (angular,$,datetimepicker,moment) {
    var module = angular.module('controllers');
    module.controller('salesIndexController', ['$scope','$rootScope','$filter','$route','userServers','businessServers','salesServers',function ($scope,$rootScope,$filter,$route,userServers,businessServers,salesServers) {
        $scope.addSalesData={};
        $scope.addSalesForm=function(valid){
            if(valid){
                $scope.addSalesData = $filter('simplifyJsonFilter')($scope.addSalesData);
                salesServers.addSales($scope.addSalesData).then(function (data) {
                    if(data){
                        $route.reload($scope.addSalesData);
                    }else{
                        alert("添加失败");
                    }
                });
            }else{
                alert("请填写全部信息");
            }
        };
        $scope.setData={};
        $scope.table_list=[];
        $scope.planList={
            branch_company_id:"",
            month:""
        };
        $scope.searchSalesForm=function(){
            $scope.planList = $filter('simplifyJsonFilter')($scope.planList);
            salesServers.searchSales($scope.planList).then(function(data) {
                $scope.table_list=data;
            });
        };
        $scope.searchSalesForm();
        // 显示模态框，给框里设置当前选中的值
        $scope.setplan=function(v){
            $scope.setSalesData=angular.copy(v);
        };
        //修改
        $scope.setForm=function(){
            $scope.setSalesData.branch_company="";
            delete $scope.setSalesData.branch_company;
            $scope.setSalesData = $filter('simplifyJsonFilter')($scope.setSalesData);
            salesServers.setplan($scope.setSalesData).then(function(data){
                if(data.data===true){
                    $route.reload();
                    $("#myModal").modal('hide');
                }else{
                    alert('失败');
                }
            });
        };
        //删除
        $scope.delplan=function(v){
            if(confirm('确定删除?')===false)
                return false;
            salesServers.delplan(v).then(function(data) {
                if(data.data===true){
                    $route.reload();
                }else{
                    alert('失败');
                }
            });                 
        };
        //全选，删除 
        $scope.checked=[];
            $scope.allSelect=function(){
                $scope.checked.forEach(function(v,i){
                    $scope.checked[i]=!v;
            });
        };
        $scope.$watch('checked',function(n,o){
                $scope.selectedData=[];
                var tempObj={};
                $scope.checked.forEach(function(v,i){
                    tempObj={};
                    if(v){
                        tempObj=$scope.table_list[i];
                        tempObj=$scope.table_list[i];
                        $scope.selectedData.push(tempObj);
                    }
                });
            },true);
        $scope.delSubmit=function(){
            if(confirm('确定删除?')===false)
                return false;                        
            console.log($scope.selectedData);            
            salesServers.delplan($scope.selectedData).then(function(data){

                if(data.data){
                    $route.reload();
                }else{
                    alert("删除失败");
                }
            });
        };
    }]);
});